// 输入系统
class InputSystem {
    constructor(world) {
        this.world = world;
        this.keys = {};
        this.touchInput = {
            up: false,
            down: false,
            left: false,
            right: false,
            shoot: false
        };
        this.isTouchDevice = 'ontouchstart' in window || navigator.maxTouchPoints > 0;
        this.joystick = null;
        this.initListeners();
    }

    // 初始化事件监听器
    initListeners() {
        // 键盘按下事件
        window.addEventListener('keydown', (e) => {
            this.keys[e.key] = true;
        });

        // 键盘释放事件
        window.addEventListener('keyup', (e) => {
            this.keys[e.key] = false;
        });
        
        // 如果是触摸设备，初始化触摸控制
        if (this.isTouchDevice) {
            this.initTouchControls();
        }
    }
    
    // 初始化触摸控制
    initTouchControls() {
        // 使用虚拟摇杆替代原有的触摸控制
        if (typeof Joystick !== 'undefined') {
            this.joystick = new Joystick(this);
        } else {
            console.error('Joystick类未找到，无法初始化虚拟摇杆');
            
            // 如果Joystick类不可用，回退到原始触摸控制
            const upButton = document.querySelector('.up-button');
            const downButton = document.querySelector('.down-button');
            const leftButton = document.querySelector('.left-button');
            const rightButton = document.querySelector('.right-button');
            const shootButton = document.querySelector('.shoot-button');
            
            if (!upButton || !downButton || !leftButton || !rightButton || !shootButton) {
                console.error('触摸控制按钮未找到');
                return;
            }
            
            // 上按钮
            upButton.addEventListener('touchstart', (e) => {
                e.preventDefault();
                this.touchInput.up = true;
            });
            upButton.addEventListener('touchend', () => {
                this.touchInput.up = false;
            });
            
            // 下按钮
            downButton.addEventListener('touchstart', (e) => {
                e.preventDefault();
                this.touchInput.down = true;
            });
            downButton.addEventListener('touchend', () => {
                this.touchInput.down = false;
            });
            
            // 左按钮
            leftButton.addEventListener('touchstart', (e) => {
                e.preventDefault();
                this.touchInput.left = true;
            });
            leftButton.addEventListener('touchend', () => {
                this.touchInput.left = false;
            });
            
            // 右按钮
            rightButton.addEventListener('touchstart', (e) => {
                e.preventDefault();
                this.touchInput.right = true;
            });
            rightButton.addEventListener('touchend', () => {
                this.touchInput.right = false;
            });
            
            // 射击按钮
            shootButton.addEventListener('touchstart', (e) => {
                e.preventDefault();
                this.touchInput.shoot = true;
            });
            shootButton.addEventListener('touchend', () => {
                this.touchInput.shoot = false;
            });
        }
    }

    // 更新系统
    update(deltaTime) {
        const localTank = this.world.getLocalTank();
        if (!localTank) return;

        // 处理输入（整合键盘和触摸输入）
        const input = {
            up: this.keys['ArrowUp'] || this.keys['w'] || this.keys['W'] || this.touchInput.up,
            down: this.keys['ArrowDown'] || this.keys['s'] || this.keys['S'] || this.touchInput.down,
            left: this.keys['ArrowLeft'] || this.keys['a'] || this.keys['A'] || this.touchInput.left,
            right: this.keys['ArrowRight'] || this.keys['d'] || this.keys['D'] || this.touchInput.right,
            shoot: this.keys[' '] || this.keys['f'] || this.keys['F'] || this.touchInput.shoot
        };

        // 更新本地坦克输入
        this.world.updateLocalInput(input);

        // 不再直接使用socket发送输入，由NetworkSystem负责处理
    }
}

// 如果在浏览器环境中，将InputSystem添加到window对象
if (typeof window !== 'undefined') {
    window.InputSystem = InputSystem;
}

// 如果在Node.js环境中，导出InputSystem类
if (typeof module !== 'undefined' && module.exports) {
    module.exports = InputSystem;
}